<html>
<head><meta charset="utf-8"><title>typeck: demand.rs · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html">typeck: demand.rs</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="193443805"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193443805" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ram <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193443805">(Apr 09 2020 at 12:40)</a>:</h4>
<p>This is for <a href="https://github.com/rust-lang/rust/issues/70851" title="https://github.com/rust-lang/rust/issues/70851">#70851</a>. How exactly would I add a condition for 16/8 bits?<br>
Sorry that it's a newbie question, but this is my first contribution and I'm not very familiar with the project yet.</p>



<a name="193448999"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193448999" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193448999">(Apr 09 2020 at 13:23)</a>:</h4>
<p>I guess a match guard (<a href="https://doc.rust-lang.org/stable/book/ch18-03-pattern-syntax.html#extra-conditionals-with-match-guards" title="https://doc.rust-lang.org/stable/book/ch18-03-pattern-syntax.html#extra-conditionals-with-match-guards">this thing</a>) could fit well</p>



<a name="193451615"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193451615" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ram <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193451615">(Apr 09 2020 at 13:43)</a>:</h4>
<p>Yes thank you, I'm using that. I should have been clearer. My question is about how do I know if found or exp is a 8 bit int? Is the condition to see if it's Some(8) for the 8 bits, or Some(1) for the 1 byte, or simply 8 or 1? And what would I be comparing it with?<br>
If I compare it with found.bit_width(), I will need to use Some(8) and see if they're equal for example.</p>



<a name="193451906"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193451906" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193451906">(Apr 09 2020 at 13:45)</a>:</h4>
<p>See <a href="https://doc.rust-lang.org/nightly/nightly-rustc/src/rustc_ast/ast.rs.html#1675-1684" title="https://doc.rust-lang.org/nightly/nightly-rustc/src/rustc_ast/ast.rs.html#1675-1684">the definition of <code>bit_width</code></a></p>



<a name="193451952"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193451952" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193451952">(Apr 09 2020 at 13:46)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">bit_width</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Option</span><span class="o">&lt;</span><span class="kt">u64</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nb">Some</span><span class="p">(</span><span class="k">match</span><span class="w"> </span><span class="o">*</span><span class="bp">self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">IntTy</span>::<span class="n">Isize</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nb">None</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">IntTy</span>::<span class="n">I8</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="mi">8</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">IntTy</span>::<span class="n">I16</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="mi">16</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">IntTy</span>::<span class="n">I32</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="mi">32</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">IntTy</span>::<span class="n">I64</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="mi">64</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">IntTy</span>::<span class="n">I128</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="mi">128</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="p">})</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</pre></div>



<a name="193452029"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193452029" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193452029">(Apr 09 2020 at 13:46)</a>:</h4>
<p>So it is the number of bits, you'll need to use <code>Some(8)</code> rather than <code>Some(1)</code></p>



<a name="193452365"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193452365" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193452365">(Apr 09 2020 at 13:49)</a>:</h4>
<blockquote>
<p>And what would I be comparing it with?</p>
</blockquote>
<p>If I'm right, the issue here is that conversions from 32+ bit integer are failible but from 8 and 16 bits integer, they are infailible.</p>



<a name="193452553"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193452553" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193452553">(Apr 09 2020 at 13:50)</a>:</h4>
<p>So you would simply need to compare with <code>16</code> to see if the comparison is fallible or not.</p>



<a name="193465068"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193465068" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ram <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193465068">(Apr 09 2020 at 15:21)</a>:</h4>
<p>Great, thank you!<br>
Do you think there's actual use in adding a match case of<br>
(Some(8), None) =&gt; true, as that's going to be caught by the (_, _) anyway? Maybe it's the other way around?</p>



<a name="193466196"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193466196" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193466196">(Apr 09 2020 at 15:28)</a>:</h4>
<p>I think the best way to do this is to add a match guard to the already existing arm</p>



<a name="193466306"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193466306" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193466306">(Apr 09 2020 at 15:29)</a>:</h4>
<p>This one:</p>
<div class="codehilite"><pre><span></span><span class="p">(</span><span class="nb">None</span><span class="p">,</span><span class="w"> </span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="p">(</span><span class="n">_</span><span class="p">,</span><span class="w"> </span><span class="nb">None</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span><span class="w"></span>
</pre></div>



<a name="193466625"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193466625" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193466625">(Apr 09 2020 at 15:31)</a>:</h4>
<p>So that we only take it if the integer is greater than 16 bits</p>



<a name="193466787"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193466787" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193466787">(Apr 09 2020 at 15:32)</a>:</h4>
<p>Ping <span class="user-mention" data-user-id="284293">@Ram</span></p>



<a name="193473960"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193473960" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ram <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193473960">(Apr 09 2020 at 16:23)</a>:</h4>
<p>Thanks! Would  something like</p>
<div class="codehilite"><pre><span></span> (None, Some(8))  | (Some(8), None)  | (None, Some(16))| (Some(16), None)   =&gt; false
</pre></div>


<p>be a cleaner way of expressing the same?</p>



<a name="193474163"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193474163" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193474163">(Apr 09 2020 at 16:24)</a>:</h4>
<p>You can rewrite that as:</p>
<div class="codehilite"><pre><span></span><span class="w"> </span><span class="p">(</span><span class="nb">None</span><span class="p">,</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="mi">8</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="mi">16</span><span class="p">))</span><span class="w">  </span><span class="o">|</span><span class="w"> </span><span class="p">(</span><span class="nb">Some</span><span class="p">(</span><span class="mi">8</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="mi">16</span><span class="p">),</span><span class="w"> </span><span class="nb">None</span><span class="p">)</span><span class="w">  </span><span class="o">=&gt;</span><span class="w"> </span><span class="kc">false</span><span class="w"></span>
</pre></div>



<a name="193474266"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193474266" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193474266">(Apr 09 2020 at 16:25)</a>:</h4>
<p>I wish we could have written that as <code>Symmetrical(None, Some(8 | 16)) =&gt; false</code></p>



<a name="193475221"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193475221" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193475221">(Apr 09 2020 at 16:32)</a>:</h4>
<p><code>Option</code> has an <code>xor</code> method.</p>



<a name="193475455"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193475455" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193475455">(Apr 09 2020 at 16:34)</a>:</h4>
<p><span class="user-mention" data-user-id="133247">@bjorn3</span> yeah, good to use here</p>



<a name="193476080"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193476080" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193476080">(Apr 09 2020 at 16:38)</a>:</h4>
<p>We could also write:</p>
<div class="codehilite"><pre><span></span><span class="p">(</span><span class="nb">None</span><span class="p">,</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">size</span><span class="p">))</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="p">(</span><span class="nb">Some</span><span class="p">(</span><span class="n">size</span><span class="p">),</span><span class="w"> </span><span class="nb">None</span><span class="p">)</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="mi">16</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span><span class="w"></span>
</pre></div>



<a name="193476151"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193476151" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193476151">(Apr 09 2020 at 16:38)</a>:</h4>
<p><span class="user-mention" data-user-id="255061">@LeSeulArtichaut</span> I don't think that's better; it's more imprecise, and in the general case, exhaustiveness checking does not work as you have a guard</p>



<a name="193476322"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193476322" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193476322">(Apr 09 2020 at 16:39)</a>:</h4>
<p>Right, in this case it's probably not better</p>



<a name="193476435"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193476435" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193476435">(Apr 09 2020 at 16:40)</a>:</h4>
<p>But if we had a bigger number of possible values, encoding them in patterns is painful <span aria-label="slight smile" class="emoji emoji-1f642" role="img" title="slight smile">:slight_smile:</span></p>



<a name="193476507"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193476507" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193476507">(Apr 09 2020 at 16:41)</a>:</h4>
<p><span class="user-mention" data-user-id="255061">@LeSeulArtichaut</span> depends on specifics, but you have range-patterns and or-patterns at your disposal to make it not painful</p>



<a name="193476585"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193476585" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193476585">(Apr 09 2020 at 16:41)</a>:</h4>
<p>In general, I'd say avoid guards if it is encodable in the pattern and it's not an obvious win</p>



<a name="193476642"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193476642" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193476642">(Apr 09 2020 at 16:42)</a>:</h4>
<p>Alright, good to know</p>



<a name="193476703"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193476703" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193476703">(Apr 09 2020 at 16:42)</a>:</h4>
<p>(ofc that's my opinion, which reasonable people may disagree with ^^)</p>



<a name="193479856"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193479856" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ram <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193479856">(Apr 09 2020 at 17:05)</a>:</h4>
<p><span class="user-mention silent" data-user-id="126931">centril</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/typeck.3A.20demand.2Ers/near/193474266" title="#narrow/stream/182449-t-compiler.2Fhelp/topic/typeck.3A.20demand.2Ers/near/193474266">said</a>:</p>
<blockquote>
<p>(None, Some(8 | 16))  | (Some(8 | 16), None)  =&gt; false</p>
<p>I wish we could have written that as <code>Symmetrical(None, Some(8 | 16)) =&gt; false</code></p>
</blockquote>
<p>Thanks <span class="user-mention" data-user-id="126931">@centril</span> ! TIL<br>
Yeah, that would be a neat way to do it.</p>



<a name="193479929"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193479929" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193479929">(Apr 09 2020 at 17:05)</a>:</h4>
<p><span class="user-mention" data-user-id="284293">@Ram</span>  you'll need to add <code>#![feature(or_patterns)]</code> to the crate if it ain't already there btw (please do, we want to test the feature internally!)</p>



<a name="193483400"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193483400" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ram <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193483400">(Apr 09 2020 at 17:32)</a>:</h4>
<p>Is this what you meant? I guess it already exists.<br>
<a href="https://github.com/rust-lang/rust/blob/master/src/librustc_typeck/lib.rs#L65" title="https://github.com/rust-lang/rust/blob/master/src/librustc_typeck/lib.rs#L65">https://github.com/rust-lang/rust/blob/master/src/librustc_typeck/lib.rs#L65</a></p>



<a name="193483447"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193483447" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193483447">(Apr 09 2020 at 17:32)</a>:</h4>
<p><span class="user-mention" data-user-id="284293">@Ram</span> yep; so nothing needs fixing there <span aria-label="slight smile" class="emoji emoji-1f642" role="img" title="slight smile">:slight_smile:</span></p>



<a name="193483476"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193483476" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ram <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193483476">(Apr 09 2020 at 17:32)</a>:</h4>
<p>BTW, is there a reason why we don't want the compiler message for 32 and 64 bit ints to be fixed too?</p>



<a name="193488089"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193488089" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ram <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193488089">(Apr 09 2020 at 18:10)</a>:</h4>
<p>Also do we want it this way whether the fn requires u8 but is given a u8 in the return; or the fn requires usize but is given u8 by the return.<br>
Is it working both ways the requirement here?</p>



<a name="193561308"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193561308" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ram <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193561308">(Apr 10 2020 at 11:52)</a>:</h4>
<p>I think this is a better way of phrasing those questions.</p>
<p>Is there a reason why we suggest 8 and 16 bit ints to use .into() but not 32/64 bit ones?</p>
<p>In the example given in the top issue comment(linked below), should it be such that the compiler suggests .into() when the fn requires a u8 but is supplied a usize instead as well? The example given is that of the fn requiring a usize but supplied a u8 instead. The difference being that u8 is likely to be &lt; usize (but is not guaranteed anyway).</p>
<p><a href="https://github.com/rust-lang/rust/issues/70851#issue-595356744" title="https://github.com/rust-lang/rust/issues/70851#issue-595356744">https://github.com/rust-lang/rust/issues/70851#issue-595356744</a></p>



<a name="193561693"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193561693" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193561693">(Apr 10 2020 at 11:59)</a>:</h4>
<p><span class="user-mention" data-user-id="284293">@Ram</span> There are no <code>From</code> impls for <code>u32</code>/<code>u64</code> -&gt; <code>usize</code></p>



<a name="193561714"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193561714" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193561714">(Apr 10 2020 at 11:59)</a>:</h4>
<p>Because <code>usize</code> might be 16 bits in size for small targets like MSP430 and AVR</p>



<a name="193561838"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193561838" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vecr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193561838">(Apr 10 2020 at 12:01)</a>:</h4>
<p>I think Rust currently defines usize/isize to be from 16 bits to 128+ bits.</p>



<a name="193561971"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193561971" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193561971">(Apr 10 2020 at 12:02)</a>:</h4>
<p>You can see the <code>From</code> and <code>TryFrom</code> implementations for <code>usize</code>/<code>isize</code> by the way</p>



<a name="193562485"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193562485" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ram <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193562485">(Apr 10 2020 at 12:10)</a>:</h4>
<p>usize depends on the target it's built for iirc so could it be 8 bits or less for some embedded systems <span class="user-mention" data-user-id="267352">@Vecr</span> ?<br>
Thanks <span class="user-mention" data-user-id="255061">@LeSeulArtichaut</span> <a href="https://doc.rust-lang.org/src/core/convert/num.rs.html#48-53" title="https://doc.rust-lang.org/src/core/convert/num.rs.html#48-53">https://doc.rust-lang.org/src/core/convert/num.rs.html#48-53</a></p>



<a name="193562587"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193562587" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vecr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193562587">(Apr 10 2020 at 12:12)</a>:</h4>
<p>I think usize implements <code>From&lt;u16&gt;</code>, but it might not on some platforms.</p>



<a name="193562593"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193562593" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193562593">(Apr 10 2020 at 12:12)</a>:</h4>
<p>The minimum is 16 bits, so <code>From&lt;u16&gt;</code> and <code>From&lt;u8&gt;</code> are still implemented for <code>usize</code></p>



<a name="193562632"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193562632" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193562632">(Apr 10 2020 at 12:12)</a>:</h4>
<p>Systems with 256 bytes of address space are not exactly useful</p>



<a name="193573224"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193573224" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193573224">(Apr 10 2020 at 14:16)</a>:</h4>
<p><span class="user-mention" data-user-id="211727">@Jonas Schievink</span> don't let the dozens of 8051's deeply embedded in your devices hear you say that</p>



<a name="193573279"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193573279" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193573279">(Apr 10 2020 at 14:16)</a>:</h4>
<p>Even that has a 16-bit address bus :P</p>



<a name="193573350"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193573350" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193573350">(Apr 10 2020 at 14:17)</a>:</h4>
<p>8-bit RAM, 16-bit ROM, I guess</p>



<a name="193573912"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193573912" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193573912">(Apr 10 2020 at 14:23)</a>:</h4>
<p><span class="user-mention" data-user-id="211727">@Jonas Schievink</span> oh dear <a href="https://www.dcd.pl/product/dq80251/" title="https://www.dcd.pl/product/dq80251/">https://www.dcd.pl/product/dq80251/</a></p>



<a name="193573982"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193573982" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193573982">(Apr 10 2020 at 14:23)</a>:</h4>
<blockquote>
<p>World’s Fastest 8051 IP Core</p>
</blockquote>
<p>oh no</p>



<a name="193640049"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193640049" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ram <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193640049">(Apr 11 2020 at 03:32)</a>:</h4>
<p>With the changes I've made now, it's such that the compiler will suggest usize be .into() when the function requires a u8.<br>
Is that fine, because usize will greater than u8 on most targets?<br>
And From(usize) is not implemented for u8/u16 anyway so that would be a bad suggestion.</p>



<a name="193651486"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193651486" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193651486">(Apr 11 2020 at 09:31)</a>:</h4>
<p>Yes, usize cannot be u8</p>



<a name="193654402"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193654402" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ram <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193654402">(Apr 11 2020 at 10:57)</a>:</h4>
<p>Yes, so would <br>
<code> (None, Some(8|16)) </code> be the correct solution rather than<br>
<code>  (None, Some(8|16)) |  (Some(8|16), None) </code><br>
because this doesn't suggest .into() when a usize is the expected type at all?<br>
Sorry if I'm not being clear <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span></p>



<a name="193654651"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/typeck%3A%20demand.rs/near/193654651" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Léo Lanteri Thauvin <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/typeck.3A.20demand.2Ers.html#193654651">(Apr 11 2020 at 11:03)</a>:</h4>
<p><span class="user-mention" data-user-id="284293">@Ram</span> I think you're right. I left you a comment on GitHub <span aria-label="slight smile" class="emoji emoji-1f642" role="img" title="slight smile">:slight_smile:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>